subroutine alloc_sim_obj(p,n)
  use table_param,only:del_sim_obj
  use table_param,only:n_del_sim_obj
  use table_param,only:sim_obj
  use table_param,only:total_alloc_sim_obj
  use table_param,only:add_alloc_sim_obj
  implicit none

  interface
     function reallocSimulatedObject(p,nold,n)
       use derived,only:SimulatedObject
       type(SimulatedObject),pointer,dimension(:) :: &
            reallocSimulatedObject,p
       integer,intent(in) :: nold,n
     end function reallocSimulatedObject

     function reallocInt(p,n)
       integer,pointer,dimension(:) :: reallocInt,p
       integer,intent(in) :: n
     end function reallocInt
  end interface

  integer,intent(in) :: p
  integer,intent(out) :: n

  integer :: na

  if (n_del_sim_obj.gt.0) then
     n=del_sim_obj(n_del_sim_obj)
     n_del_sim_obj=n_del_sim_obj-1
  else
     if (p.gt.total_alloc_sim_obj) then
        na=total_alloc_sim_obj+add_alloc_sim_obj
        sim_obj=>reallocSimulatedObject(sim_obj,&
             total_alloc_sim_obj,na)
        del_sim_obj=>reallocInt(del_sim_obj,na)
        total_alloc_sim_obj=na
     end if
     n=p
  end if

  return

end subroutine alloc_sim_obj
